<template>
  <view class="facial-verification-page">
    <view class="body">
      <view class="title">
        <text>人脸核身</text>
      </view>
      <u-form class="form" labelPosition="left" :model="user" ref="uForm" labelWidth="100">
        <u-form-item label="姓名" prop="name" borderBottom ref="name" required>
          <u-input v-model="user.idName" border="none"></u-input>
        </u-form-item>
        <u-form-item label="身份证号" prop="idCard" borderBottom ref="idCard" required>
          <u-input v-model="user.idNumber" border="none"></u-input>
        </u-form-item>
        <view class="form-btns" style="margin-top: 20rpx">
          <view class="form__btns_item">
            <u-button type="primary" color="#FF7947" shape="circle" @click="handleToProgram">开始人脸核身</u-button>
          </view>
          <!--<view class="form__btns_item" style="margin-top: 20rpx">-->
          <!--  <u-button type="primary" color="#FF7947" shape="circle"-->
          <!--            @click="getHandleResult()">查询结果-->
          <!--  </u-button>-->
          <!--</view>-->
        </view>
      </u-form>
      <p style="margin-top: 20rpx">{{result}}</p>
      <image :src="imgSrc" alt="" style="width: 200px;height: 200px; margin-top: 20rpx; text-align: center"/>
    </view>


  </view>
</template>

<script>

import {aseEncryptParams, generateUUID} from "@/utils/common";

export default {
  name: "facial-verification",
  data() {
    return {
      user: {
        idName: '刘逸诗',
        idNumber: '360723199109190019'
      },
      token: null,
      imgSrc: "",
      result: ""
    }
  },
  onShow: function () {
    const data = wx.getEnterOptionsSync()
    if(data && data.referrerInfo && data.referrerInfo.extraData) {
      const result = data.referrerInfo.extraData.data;
      this.result = result;
      const res = JSON.parse(result);
      if (res.success) {
        const outside_no = res.outside_no;
        this.getHandleResult(outside_no)
      }
    }
  },
  onLoad(options) {
    this.init() // 初始化获取token
  },
  methods: {
    // 获取处理结果
    getHandleResult(outside_no) {
      const url = "https://gauss.shargoodata.com/gauss/mp/ocr/query";
      const data = {
        platformNo: 'S0382',
        body: outside_no
      }
      const _this = this;
      wx.request({
        url: url,
        data: data,
        method: 'POST',
        header: {
          // 'content-type': 'multipart/form-data',
          'content-type': 'application/x-www-form-urlencoded',
          'token': this.token
        },
        success: function (res) {
          console.log(res)
          _this.imgSrc = "data:image/png;base64," + res.data.data.image
        }
      })
    },
    init() {
      const _this = this;
      wx.request({
        url: 'https://gauss.shargoodata.com/gauss/authorization/token.json',
        data: {
          platformNo: 'S0382',
          secretKey: 'GtGpueCETEUCYIBFrc0toFGVplKL4d4D'
        },
        dataType: 'JSON',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        success: function (res) {
          _this.token = JSON.parse(res.data).data
        }
      })
    },
    handleToProgram() {
      const uuid = "AEE0D881-80C8-100E-CEDB-5312F6487479"
      // 组装data
      const data = {
        token: this.token,
        platformNo: 'S0382',
        secretKey: 'GtGpueCETEUCYIBFrc0toFGVplKL4d4D',
        orderNo: generateUUID(),
        extraInfo: {
          isShowCamera: true,
          idName: this.user.idName,
          idNumber: this.user.idNumber
        }
      }
      console.log(data)
      wx.navigateToMiniProgram({
        appId: 'wxe83fe5a4f77efb58',   // 跳转到另外一个小程序的appId（复制即可）
        path: 'pages/source/index?id=' + uuid,   // 跳转到小程序的页面（复制即可）
        extraData: {
          params: aseEncryptParams(JSON.stringify(data), uuid)
        },  // data需要携带的信息，具体请见详情
        envVersion: 'release',  // 跳转的环境， 支持的枚举 envVersion
        success(res) {
          // console.log(res)
        }
      })
    }
  }

}
</script>


<style scoped lang="scss">
.facial-verification-page {
  .body {
    margin: 20rpx;
    padding: 20rpx;
    border-radius: 8rpx;
    background-color: #fff;

    .title {
      margin-bottom: 10rpx;
      font-size: 32rpx;
      font-weight: bold;
    }

    .form {
      .form-btns {
        margin-top: 20rpx;
      }
    }
  }

}

</style>